#include "pch.h"
#include "SqlHelp.h"

void SqlHelp::Init()
{
	pConn.CreateInstance(__uuidof(Connection));
	pRec.CreateInstance(__uuidof(Recordset));
}
void SqlHelp::Release()
{
	pRec.Release();
	pConn.Release();
}
void SqlHelp::Connect(string Provider, string DataSource, string InitialCatalog, string UserID, string Password)
{
	//Provider = SQLOLEDB.1;Data Source = 192.168.10.35;User ID = sa;Password=d;Initial Catalog = tempdb
	string connStr("Provider=");
	connStr.append(Provider);
	connStr.append(";Data Source=");
	connStr.append(DataSource);
	connStr.append(";Initial Catalog=");
	connStr.append(InitialCatalog);
	connStr.append(";User ID =");
	connStr.append(UserID);
	connStr.append(";Password=");
	connStr.append(Password);
	pConn->ConnectionString = _bstr_t(connStr.c_str());
	pConn->Open("", "", "", adModeUnknown);
}
void SqlHelp::Connect(string DataSource, string InitialCatalog, string UserID, string Password)
{
	Connect("SQLOLEDB.1", DataSource, InitialCatalog, UserID, Password);
}
void SqlHelp::Exec(string SQL)
{
	if (pRec->State)
	{
		pRec->Close();
	}
	pRec->CursorLocation = adUseClient;
	pRec->Open(_bstr_t(SQL.c_str()), _variant_t((IDispatch*)pConn), adOpenDynamic, adLockOptimistic, adCmdText);
}
void SqlHelp::GetRowItem(string Name, string& OutItem)
{
	if (pRec->EndOfFile == false)
	{
		OutItem = (char*)_bstr_t(pRec->GetCollect(_bstr_t(Name.c_str())));
	}
	else
	{
		OutItem = "EOF";
	}
}
void SqlHelp::MoveNext()
{
	pRec->MoveNext();
}
bool SqlHelp::IsEOF()
{
	return pRec->EndOfFile;
}

long SqlHelp::GetCount()
{
	m_Count = pRec->GetRecordCount();
	return m_Count;
}

SqlHelp::SqlHelp()
{
	m_Count = -1;
}
